let that;

class employeeSystem {
    //构造器
    constructor(tableName, addBtn, btn_return, btn_add, btn_search) {
        that = this;
        //初始化元素
        this.table = document.querySelector(tableName);
        if (addBtn == null || addBtn == undefined) {
            addBtn = ".add";
        }
        this.addBtn = document.querySelector(addBtn);
        // console.log(that.addBtn);//已成功获取
        // console.log(this.addBtn);//已成功获取
        if (btn_return == null || btn_return == undefined) {
            btn_return = "btn_return";
        }
        this.btn_return = document.getElementById(btn_return);
        // console.log(this.btn_return);//已捕获成功
        if (btn_add == null || btn_add == undefined) {
            btn_add = "btn_add";
        }
        this.btn_add = document.getElementById(btn_add);
        //自定义属性
        this.btn_add.onOff = false;
        // console.log(this.btn_add);//已捕获成功
        if (btn_search == null || btn_search == undefined) {
            btn_search = "search";
        }
        this.btn_search = document.getElementById(btn_search);
        //绑定事件
        this.bind();
    }


    bind() {
        //通过循环获得删除和修改两个按键
        this.delete_btn = document.querySelectorAll(".sp_delete");
        // console.log(this.delete_btn);//已捕获成功
        this.modify_btn = document.querySelectorAll(".sp_modify");
        // console.log(this.modify_btn);//已捕获成功
        //获取input输入框的值
        let input = document.querySelectorAll(".control p input");
        //添加事件
        this.addBtn.onclick = this.formShow;
        this.btn_return.onclick = this.formNone;
        this.btn_search.onclick = this.searchBy;

        //添加循环
        for (let i = 1; i < that.table.rows.length; i++) {
            //获取当前行
            let current_row = that.table.rows[i];
            let delete_btn = this.delete_btn;
            let modify_btn = this.modify_btn;
            // console.log(current_row.rowIndex);//已捕获成功
            delete_btn[i - 1].onclick = function () {
                that.table.deleteRow(current_row.rowIndex);
                // console.log(current_row.rowIndex);//已捕获成功
            };
            modify_btn[i - 1].onclick = function () {
                that.formShow();
                for (let j = 1;j < input.length;j ++){
                    input[j].value = current_row.cells[j-1].innerHTML;
                }
                input[0].value = input[1].value;
                input[1].readOnly = true;
                //获取btn_add，在修改的情况下点击btn_add时
                that.btn_add.innerHTML = "alter";
                document.getElementById('control').action = '/MyWeb/employee/update';
                console.log(document.getElementById('control'))
            };
            input[1].readOnly = false;
            that.btn_add.innerHTML = "add";
        }
    }

    formShow() {
        //获取表单
        let control = document.querySelector(".control");
        control.style.display = "block";
        //重新绑定事件
        that.bind();
    }

    formNone() {
        //获取表单
        let control = document.querySelector(".control");
        //获取input输入框的值
        let input = document.querySelectorAll(".control p input");
        //清空input的值
        for (let i = 0; i < input.length; i++) {
            input[i].value = "";
        }
        control.style.display = "none";
        //重新绑定事件
        that.bind();
    }

    searchBy() {
        //获取input输入框的值
        let input = document.querySelectorAll(".search p input");
        let searchName = input[0];
        let searchGender = input[1];
        let gender;
        //获取当前行
        for (let i = 1; i < that.table.rows.length; i++) {
            let current_row = that.table.rows[i];
            current_row.className = "";
            if (searchName.value.trim() == current_row.cells[1].innerHTML) {
                console.log(current_row);
                console.log(searchGender.checked);
                //设置一个判断条件
                if (searchGender.checked) {
                    gender = 1;
                } else {
                    gender = 0;
                }
                console.log(gender);
                console.log(current_row.cells[3].innerHTML);
                if (gender == current_row.cells[3].innerHTML) {
                    console.log(111);
                    current_row.className = "success";
                }
            }
        }
    }
}